package com.gitee.zeric.tenantdb.demo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gitee.zeric.tenantdb.annotation.AutowiredDataSource;
import com.gitee.zeric.tenantdb.demo.entity.CustomResult;
import com.gitee.zeric.tenantdb.demo.entity.Sample;
import com.gitee.zeric.tenantdb.demo.service.SampleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 测试数据接口
 * @author zj
 * @date 2022/11/25
 */
@RestController
@RequestMapping("/sample")
public class SampleController {

    @Autowired
    private SampleService sampleService;


    /**
     * 匹配租户一，默认数据库
     *
     * @return {@link CustomResult}<{@link Sample}>
     */
    @GetMapping("/muti-demo1")
    @AutowiredDataSource(tenandId = "1")
    public CustomResult<Sample> getInfo() {
        List<Sample> userList = sampleService.list(new QueryWrapper<Sample>().last(" limit 3"));
        return CustomResult.Ok(userList);
    }

    /**
     * 匹配租户一，指定数据库
     *
     * @return {@link CustomResult}<{@link Sample}>
     */
    @GetMapping("/muti-demo2")
    @AutowiredDataSource(tenandId = "1",database = "multi-demo2")
    public CustomResult<Sample> getInfo2() {
        List<Sample> userList = sampleService.list(new QueryWrapper<Sample>().last(" limit 3"));
        return CustomResult.Ok(userList);
    }
}
